import { fetchData,deleteData,searchData,updateInfo} from "../services/api";

const person= {
    namespace: 'person',
    state:{
      personList: [],
    },
    reducers: {
      updateData(state, {payload}){
        return{
          ...state,
          personList: payload
        }
      },
    },
    effects: {
      // 异步获取数据
      *fetchData({payload}, {call,put}){
        const res = yield call(fetchData);
        yield put({
          type: 'updateData',
          payload: res.data.data
        });
      },
      // 根据id删除person
      *deletePersonById({payload},{call,put}){
        const res = yield call(deleteData,payload);
        yield put({
          type: 'updateData',
          payload: res.data.data
        });
      },
      // 查询person
      *searchPerson({payload},{call,put}){
        const res = yield call(searchData,payload);
        yield put({
          type: 'updateData',
          payload: res.data.data
        });
      },
      // 修改person信息
      *updatePersonInfo({payload},{call,put}){
        const res = yield call(updateInfo,payload);
        yield put({
          type: 'updateData',
          payload: res.data.data
        });
      }
    }
  };

export default person;
